home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FIRE_TUT.ZIP / FIRE.C < prev    next >
C/C++ Source or Header  |  1995-08-11  |  4KB  |  186 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include "fire.h"
  5.  
  6. int main()
  7. {
  8.  int x=0,y=0;
  9.  int ch = ' '; //== More variables... ==//
  10.  
  11.  atexit(b4exit);
  12.  startmsg();
  13.  set_mx();    //== Get into Mode X ==//
  14.  randomize(); //== Guess... ==//
  15.  set_pal();   //== Set our palette ==//
  16.  
  17. //== Initialize both arrays to 0 ==//
  18.  for (y = 0; y <= 56; y++)
  19.   {
  20.    for (x = 0; x <= 80; x++)
  21.     {
  22.      current[y][x] = 0;
  23.      working[y][x] = 0;
  24.     }
  25.   }
  26.  
  27.  do
  28.   {
  29.    flame(); //== Call the flame generator ==//
  30.    if(kbhit()) //== See if user hit keyboard ==//
  31.     {
  32.      ch=getch();
  33.     }
  34.   } while (ch != 27);
  35.  
  36.  return(0);
  37. }
  38.  
  39.  
  40. void flame()
  41. {
  42.  int delta=0,i=0,x=0,y=0; //== General loops ==//
  43.  int cl1=0,cl2=0,cl3=0,cl4=0,cl5=0,cl6=0;  //== color vals used in averaging ==//
  44.  
  45. //== generate bottom line of flame ==//
  46.  for(i=0;i<80;i++)
  47.   {
  48.    if(random(10) < 5)
  49.     {
  50.      delta=random(2)*255;
  51.     }
  52.    working[55][i]=delta;
  53.    working[56][i]=delta;
  54.   }
  55.  
  56. //== Get averages for pixels surrounding a given pixel ==//
  57.  for (y = 55; y >= 2; y--)
  58.   {
  59.    for (x = 1; x <= 79; x++)
  60.     {
  61.      cl1 = current[y + y_amt_1][x + x_amt_1];
  62.      cl2 = current[y + y_amt_2][x + x_amt_2];
  63.      cl3 = current[y + y_amt_3][x + x_amt_3];  //== Get your five numbers to
  64.      cl4 = current[y + y_amt_4][x + x_amt_4];  //== use in averaging
  65.      cl5 = current[y + y_amt_5][x + x_amt_5];
  66.      cl6 = (cl1+cl2+cl3+cl4+cl5) /5;
  67.  
  68.      if (cl6 > opt_decay_at) cl6 -= opt_decay_by;  //== Decay value, or it would never fade ==//
  69.      working[y-1][x] = cl6;  //== Assign the averaged color to the working array ==//
  70.     }
  71.   }
  72.  
  73. //== Copy current[][] to working[][] ==//
  74.  for (y =0; y <= 56; y++)
  75.   {
  76.    for (x = 0; x <= 80; x++)
  77.     {
  78.      current[y][x] = working[y][x];
  79.     }
  80.   }
  81.  
  82. //== Dump current[][] to screen ==//
  83.  dump2con();
  84. }
  85.  
  86.  
  87. void dump2con()
  88. {
  89. //== Write the buffer to the screen ==//
  90.  
  91.  _SI = (unsigned int)¤t[0][0];
  92.  asm mov di,0
  93.  asm mov ax,0A000h
  94.  asm mov es,ax
  95.  asm mov cx,40*45  /* Change the 40 to 56 if you don't want the
  96.               flame to be cropped */
  97. XF1:
  98.  asm mov ax,ds:[si]
  99.  asm add si,2
  100.  asm mov dl,al
  101.  asm mov ax,ds:[si]
  102.  asm add si,2
  103.  asm mov dh,al
  104.  asm mov es:[di],dx
  105.  asm add di,2
  106.  asm dec cx
  107.  asm jnz XF1
  108. }
  109.  
  110.  
  111. void set_pal()
  112. {
  113. //== First we shift our palette values... ==//
  114.  int  i;
  115.  
  116.  for(i=0;i<768;i++)
  117.   {
  118.    pal[i] = pal[i] >> pal_darkness;
  119.   }
  120.  
  121. //== ...then set our palette ==//
  122.  _SI = (unsigned int)&pal[0];
  123.  asm mov cx,768
  124.  asm mov dx,0x03c8
  125.  asm xor al,al
  126.  asm out dx,al
  127.  asm inc dx
  128. l1:
  129.  asm outsb
  130.  asm dec cx
  131.  asm jnz l1
  132. }
  133.  
  134.  
  135. void set_mx(void)
  136. {
  137. //== Your generic routine to set ModeX ==//
  138.  asm CLD
  139.  asm MOV AX,13h
  140.  asm INT 10h
  141.  asm CLI
  142.  asm MOV DX,3c4h
  143.  asm MOV AX,604h // Unchain VGA
  144.  asm OUT DX,AX
  145.  asm MOV AX,0F02h // All planes
  146.  asm OUT DX,AX
  147.  
  148.  asm MOV DX,3D4h
  149.  asm MOV AX,14h // Disable dword mode
  150.  asm OUT DX,AX
  151.  asm MOV AX,0E317h // Enable byte mode.
  152.  asm OUT DX,AX
  153.  asm MOV AL,9
  154.  asm OUT DX,AL
  155.  asm INC DX
  156.  asm IN  AL,DX
  157.  asm AND AL,0E0h // Duplicate each scan 8 times.
  158.  asm ADD AL,7
  159.  asm OUT DX,AL
  160. }
  161.  
  162.  
  163. void b4exit()
  164. {
  165.  asm mov ax,03h
  166.  asm int 10h
  167. }
  168.  
  169.  
  170. void startmsg()
  171. {
  172.  printf(
  173.     "┌───────────────────────────────────────────────────────────────┐\n"
  174.     "│                                                     8-8-95    │\n"
  175.     "│       Fire Demo Tutorial                                      │\n"
  176.     "│       By Wikki of Fragmentaria                                │\n"
  177.     "│       Contact me at:                                          │\n"
  178.     "├───────────────────────────────────────────────────────────────┤\n"
  179.     "│           AARONC@BBS.GEMLINK.COM                              │\n"
  180.     "├───────────────────────────────────────────────────────────────┤\n"
  181.     "│       Or e-mail Aaron Clemmer on this BBS:                    │\n"
  182.     "│           Locust Grove BBS                                    │\n"
  183.     "│           1(540)672-8760  8-N-1                               │\n"
  184.     "└───────────────────────────────────────────────────────────────┘\n");
  185.  getch();
  186. }